package com.yuwei.yurpc;

import com.yuwei.yurpc.config.RegistryConfig;
import com.yuwei.yurpc.config.RpcConfig;
import com.yuwei.yurpc.constant.RpcConstant;
import com.yuwei.yurpc.registry.Registry;
import com.yuwei.yurpc.registry.RegistryFactory;
import com.yuwei.yurpc.utils.configUtils;

/**
 * RPC应用
 * 全局配置加载
 *
 * @author yuwei
 * @date 11:11 2024/10/4
 */
public class RpcApplication {
    private static volatile RpcConfig rpcConfig;
    public static void init(){
     RpcConfig newRpcConfig = null;

     try{
         newRpcConfig = configUtils.loading(RpcConfig.class, RpcConstant.CONFIG_PREFIX,"");

     }catch (Exception e){
         e.printStackTrace();
     }
    if(newRpcConfig != null){
        rpcConfig = newRpcConfig;
    }else {
        rpcConfig = new RpcConfig();
    }
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        registry.init(registryConfig);

        registry.heartBeat();


        //创建并注册 ShutdownHook
        Runtime.getRuntime().addShutdownHook(new Thread(registry::destroy));
    }
    public static RpcConfig getRpcConfig(){
       if(rpcConfig==null){
           synchronized (RpcApplication.class){
               if(rpcConfig==null){
                   init();
               }
           }
       }
        return rpcConfig;
    }
    public static void main(String[] args) {
    }


}
